import { assert } from "../common/assert.js"
import { isNumber } from "../data-type/is.js"
import { getLastIndexOf } from "./getLastIndexOf.js";

const START = 'start';
const END = 'end';
/**
 * 获取数组对应值
 * @param {*} data 
 * @param {*} position 
 */
export function getValueOf(data, position, hook) {
    hook && hook()
    if (position === START) {
        return data[0]
    }

    if (position === END) {
        return data[getLastIndexOf(data)]
    }

    assert(
        isNumber(position) &&
        position >= 0 &&
        position <= getLastIndexOf(data), 'invalid array index!')
    return data[position];
}